/*
 * Copyright (c) 2012 Johan Fylling
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy 
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights 
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 
 * copies of the Software, and to permit persons to whom the Software is 
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in 
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 
 * THE SOFTWARE.
 */

part of engine;


abstract class ParticleTouchListener {
  void particleTouched (Particle p);
}

class Particle {
  num                   _mass;
  num                   massInv;
  final Vector          _position;
  final Vector          _positionPrev;
  final Vector          _force;
  ParticleTouchListener touchListener = null;
  
  Particle (num mass, Vector position, 
            [ParticleTouchListener this.touchListener]) : this._position = new Vector.copy(position),
                                                          this._positionPrev = new Vector.copy(position),
                                                          this._force = new Vector.copy(position) {
    this.mass = mass;
    this._force.setNull();
  }
  
  num get mass => this._mass;
  
  /**
   * Sets the mass (and, implicitly the massInv) of this Particle.
   */
  void set mass (num m) {
    this._mass = m;
    this.massInv = (m != 0 ? 1 / m : 0);
  }
  
  Vector get position => this._position;
  
  void getPosition (Vector other) {
    other.clone(this._position);
  }
  
  /**
   * Copies the provided Vector into the Particle's position.
   */
  void set position (Vector other) {
    this._position.clone(other);
    if (this.touchListener != null) {
      this.touchListener.particleTouched(this);
    }
  }
  
  Vector get positionPrev => this._positionPrev;
  
  void getPositionPrev (Vector other) {
    other.clone(this._positionPrev);
  }
  
  void set positionPrev (Vector other) => this._positionPrev.clone(other);
  
  Vector get force => new Vector2.copy(this._force);
  
  void set force (Vector other) => this._force.clone(other);
  
  String toString() => "p[$_mass/$massInv,$_positionPrev=>$_position,$_force]";
}
